/*
 * ArCoLIVE - Live Internet Videoconference Environment
 * Information Technology Departament - Federal University of Alagoas - Brazil
 *
 * Copyright (C) 2005 Felipe B. Pontes (felipe@labpesquisas.tci.ufal.br)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package br.ufal.tci.nexos.arcolive.participant;

import java.net.InetAddress;
import java.net.Socket;
import java.util.Date;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

import javax.media.rtp.SessionAddress;

import br.ufal.tci.nexos.arcolive.ArCoLIVEServiceInstance;
import br.ufal.tci.nexos.arcolive.descriptors.ParticipantDescriptor;
import br.ufal.tci.nexos.arcolive.events.ParticipantConnectionEvent;
import br.ufal.tci.nexos.arcolive.exception.ArCoLIVEException;
import br.ufal.tci.nexos.arcolive.protocol.ArCoLIVEProtocol;
import br.ufal.tci.nexos.arcolive.service.connection.ConnectionListener;
import br.ufal.tci.nexos.arcolive.service.connection.ConnectionService;

/**
 * CLASSNAME.java
 *
 * CLASS DESCRIPTION
 *
 * @see CLASSNAME
 *
 * @author <a href="mailto:felipe@labpesquisas.tci.ufal.br">Felipe Barros Pontes</a>.
 * @author <a href="mailto:leandro@labpesquisas.tci.ufal.br">Leandro Melo de Sales</a>.
 * @since 0.1
 * @version 0.1
 *
 * <p><b>Revisions:</b>
 *
 * <p><b>yyyymmdd USERNAME:</b>
 * <ul>
 * <li> VERSION
 * </ul>
 */

public class ArCoLIVEParticipant implements ConnectionListener {
	private SessionAddress sessionAddressRTP;
	private SessionAddress sessionAddressRTCP;
	private Set listeners;
	private Hashtable serviceProtocols;
	private ConnectionService connectionService;
	private ParticipantDescriptor participantDescriptor;
	
	public ArCoLIVEParticipant(Socket socket) throws ArCoLIVEException {
		this.listeners = new HashSet();
		this.serviceProtocols = new Hashtable();
		this.connectionService = new ConnectionService(socket);
		this.connectionService.addConnectionListener(this);
		this.connectionService.startService();
		Date date = new Date();
		this.participantDescriptor = new ParticipantDescriptor(0, date.getTime());
	}
	
	public void addListener(ParticipantListener listener) {
		this.listeners.add(listener);
	}
	
	public void addServiceProtocol(ArCoLIVEServiceInstance service, ArCoLIVEProtocol protocol) {
		this.serviceProtocols.put(service, protocol);
	}
	
	public ArCoLIVEProtocol getServiceProtocol(ArCoLIVEServiceInstance service) {
		return (ArCoLIVEProtocol)this.serviceProtocols.get(service);
	}
	
	public InetAddress getInetAddress() {
		return this.connectionService.getConnection().getInetAddress();
	}
	
	public ConnectionService getConnectionService() {
		return this.connectionService;
	}
	
	public SessionAddress createSessionAddressRTP(int port) {
		if (this.sessionAddressRTP == null)
			this.sessionAddressRTP = new SessionAddress(this.getInetAddress(), port);
		return this.sessionAddressRTP;
	}
	
	public SessionAddress createSessionAddressRTCP(int port) {
		if (this.sessionAddressRTCP == null)
			this.sessionAddressRTCP = new SessionAddress(this.getInetAddress(), port);
		return this.sessionAddressRTCP;
	}

	public void connectionParticipantEvent(ParticipantConnectionEvent event) {
		for (Iterator iter = this.listeners.iterator(); iter.hasNext();) {
			event.setSource(this);
			((ParticipantListener)iter.next()).participantEvent(event);
		}
	}

	public ParticipantDescriptor getParticipantDescriptor() {
		return participantDescriptor;
	}
	
	public boolean equals(Object object) {
		if (object instanceof ArCoLIVEParticipant) {
			return (this.participantDescriptor.getParticipantId() == ((ArCoLIVEParticipant)object).getParticipantDescriptor().getParticipantId());
		} else {
			return false;
		}
	}
	
	/*public String toString() {
		String participantEmail = this.participantDescriptor.getParticipantEmail();
		if (participantEmail.equals("")) {
			return "<Participant E-mail not specified>";
		}
		return participantEmail;
	}*/
	
}